<template>
  <Navbar></Navbar>
  <!-- 滚动容器 -->
  <scroll-view scroll-y @scrolltolower="onScrolltolower" class="scroll" :refresher-triggered="isTriggered" @refresherrefresh="onRefresher" refresher-enabled>
    <XtxSwiper :list="bannerList"></XtxSwiper>
    <Category :list="categoryList"></Category>
    <HotPanel :list="hotList"></HotPanel>
    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef"/>
  </scroll-view>

</template>

<script setup lang="ts">

import Navbar from './componets/CustomNavbar.vue'
import Category from './componets/CategoryPanel.vue'
import HotPanel from './componets/HotPanel.vue'
import type { XtxGuessInstance } from '@/types/components'
import { ref } from 'vue'
import { getHomeBannerAPI,getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem,CategoryItem, HotItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'

//获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

//获取分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

//获取热门推荐数据
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

onLoad(async () => {
  await Promise.all([getHomeBannerData(),getHomeCategoryData(),getHomeHotData()])
}) 

// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动触底事件
const onScrolltolower = () => {
  guessRef.value?.getMore()
}

const isTriggered = ref(false)

const onRefresher = async() => {
  isTriggered.value = true
  guessRef.value?.resetData()
  guessRef.value?.getMore()
  await Promise.all([getHomeBannerData(),getHomeCategoryData(),getHomeHotData()])
  isTriggered.value = false
}

</script>

<style lang="scss">

page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;

  .scroll {
    flex: 1;
    height: 0;
  }
} 

</style>
